package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.StoreGoodsOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 加订表 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2024-07-30
 */
public interface StoreGoodsOrderMapper extends BaseMapper<StoreGoodsOrder> {


    @Select("select * from store_goods_order where batch_num = ${batchNum} and deleted = 0")
    public List<StoreGoodsOrder> selectListForBatchNum(@Param("batchNum") Long batchNum);


    @Select({"<script>",
            "SELECT",
            "IFNULL( MAX(t1.lesson_num),0) as lesson_num",
            "FROM",
            "store_goods_order AS t1",
            "INNER JOIN course_type AS t2 ON t1.class_type_id = t2.id",
            "INNER JOIN organization AS t3 ON t1.school_id = t3.id",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
            "and t1.school_id = ${schoolId}",
            "</when>",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
            "and t1.subjects_id = ${subjectsId}",
            "</when>",
            "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
            "and t1.year_part = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
            "and t1.quarter_num = ${quarterNum}",
            "</when>",
            "<when test='yearClassId!=null and yearClassId!=&apos;&apos; '>",
            "and t1.year_class_id = ${yearClassId}",
            "</when>",
            "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
            "and t1.class_type_id = ${classTypeId}",
            "</when>",
            "and t1.goods_type_id = ${goodsTypeId}",
            "and t1.deleted =0 ORDER BY t1.id desc",
            "</script>"})
    public Integer getMaxLessonNum(@Param("yearPart") Integer yearPart,
                                               @Param("quarterNum") Integer quarterNum,
                                               @Param("schoolId") Long schoolId,
                                               @Param("yearClassId") Integer yearClassId,
                                               @Param("classTypeId") Long classTypeId,
                                               @Param("subjectsId") Long subjectsId,
                                               @Param("lessonType") Integer lessonType,
                                               @Param("goodsTypeId") Long goodsTypeId,
                                               @Param("status") Integer status,
                                               @Param("type") Integer type
    );


    @Select({"<script>",
            "SELECT",
                    "t1.*,",
                    "t2.NAME AS class_type_name,",
                    "t3.NAME AS school_name,",
                    "getDictName ( t1.year_class_id, 14 ) AS year_class_name ",
                    "FROM",
                    "store_goods_order AS t1",
                    "INNER JOIN course_type AS t2 ON t1.class_type_id = t2.id",
                    "INNER JOIN organization AS t3 ON t1.school_id = t3.id",
                    "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
                        "and t1.school_id = ${schoolId}",
                    "</when>",
                    "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
                        "and t1.subjects_id = ${subjectsId}",
                    "</when>",
                    "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
                        "and t1.year_part = ${yearPart}",
                    "</when>",
                    "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
                        "and t1.quarter_num = ${quarterNum}",
                    "</when>",
                    "<when test='yearClassId!=null and yearClassId!=&apos;&apos; '>",
                        "and t1.year_class_id = ${yearClassId}",
                    "</when>",
                    "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
                        "and t1.class_type_id = ${classTypeId}",
                    "</when>",
                    "and t1.goods_type_id = ${goodsTypeId}",
                    "and t1.deleted =0 ORDER BY t1.id desc",
            "</script>"})
    public IPage<StoreGoodsOrder> getOrderList(IPage<?> page,
                                              @Param("yearPart") Integer yearPart,
                                              @Param("quarterNum") Integer quarterNum,
                                              @Param("schoolId") Long schoolId,
                                              @Param("yearClassId") Integer yearClassId,
                                              @Param("classTypeId") Long classTypeId,
                                              @Param("subjectsId") Long subjectsId,
                                              @Param("lessonType") Integer lessonType,
                                              @Param("goodsTypeId") Long goodsTypeId,
                                              @Param("status") Integer status,
                                              @Param("type") Integer type
                                              );


    /**
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param yearClassId
     * @param classTypeId
     * @param subjectsId
     * @param lessonType
     * @param goodsTypeId
     * @param status
     * @param type
     * @param getType 2为按batch_num分组
     * @return
     */
    @Select({"<script>",
            "SELECT",
            "t1.*,",
            "t2.NAME AS class_type_name,",
            "t3.NAME AS school_name,",
            "getDictName ( t1.year_class_id, 14 ) AS year_class_name ",
            "FROM",
            "store_goods_order AS t1",
            "INNER JOIN course_type AS t2 ON t1.class_type_id = t2.id",
            "INNER JOIN organization AS t3 ON t1.school_id = t3.id",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
                "and t1.school_id = ${schoolId}",
            "</when>",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
                "and t1.subjects_id = ${subjectsId}",
            "</when>",
            "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
                "and t1.year_part = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
                "and t1.quarter_num = ${quarterNum}",
            "</when>",
            "<when test='yearClassId!=null and yearClassId!=&apos;&apos; '>",
                "and t1.year_class_id = ${yearClassId}",
            "</when>",
            "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
                "and t1.class_type_id = ${classTypeId}",
            "</when>",
                "and t1.goods_type_id = ${goodsTypeId}",
                "and t1.deleted =0",
            "<when test='getType!=null and getType!=&apos;&apos; and getType ==2'>",
                "GROUP BY t1.batch_num",
            "</when>",
                "ORDER BY t1.id desc",
            "</script>"})
    public List<StoreGoodsOrder> getOrderListAll(
                                               @Param("yearPart") Integer yearPart,
                                               @Param("quarterNum") Integer quarterNum,
                                               @Param("schoolId") Long schoolId,
                                               @Param("yearClassId") Integer yearClassId,
                                               @Param("classTypeId") Long classTypeId,
                                               @Param("subjectsId") Long subjectsId,
                                               @Param("lessonType") Integer lessonType,
                                               @Param("goodsTypeId") Long goodsTypeId,
                                               @Param("status") Integer status,
                                               @Param("type") Integer type,
                                               @Param("getType") Integer getType
    );
}
